CS475/505 Assignment 3

Constraint Satisfaction Problems

 

Goal: write a CSP solver for Sudoku puzzles, using standard backtracking, and the use of forward checking and the minimum remaining value heuristic.

Sudoku puzzles are played on a 9 x 9 grid of digits. The grid starts out with some digits already set. The aim is to complete the grid by placing the digits 1 through 9 in the remaining grid locations so that each row, each column and each 3 x 3 square has all different digits. There is almost always only one solution. Here is a sample puzzle and its solution.

- 8 6 - - - - 1 -

7 - 5 - - 8 - - -

- - 2 - - - - - 4

- - 4 - - - 3 6 -

- - 8 5 - - - - -

- - - 1 - - - - -

- - - - - - - 8 1

8 - - - - 4 2 - -

- 2 - 6 - 3 - - -

-----------------

4 8 6 3 5 9 7 1 2

7 1 5 4 2 8 6 9 3

3 9 2 7 6 1 8 5 4

1 7 4 8 9 2 3 6 5

2 3 8 5 4 6 1 7 9

5 6 9 1 3 7 4 2 8

6 4 3 2 7 5 9 8 1

8 5 7 9 1 4 2 3 6

9 2 1 6 8 3 5 4 7

Representation

Represent the puzzle as a CSP with 81 variables, one for each of the grid locations. The CSP will require a set of constraints based on the rules of the puzzle. It will also need a set of domains for each variable.

Write a suitable ‘factory’ method for setting up a puzzle with the initial assignments so that you can run the algorithm on any number of puzzles.

The CSP Search

1.       Write the standard backtracking CSP search or use the code in the AIMA package. Have the algorithm measure both the number of nodes visited (the number of variables assigned) and the CPU time.

2.       Augment the algorithm with code for forward checking and the minimum remaining values heuristic. Forward checking involves removal of values from the domain of variables which are affected (through constraints) by the assignment of a value to a variable. This makes the domain of each variable a dynamic entity. Minimum remaining values is a way of choosing the next best variable to assign based on the size of its domain. When the algorithm backtracks because a domain for a chosen variable is empty, the reduction of domains must be undone.

Sample puzzles

Your code must solve the following three puzzles. Of course it should work on any puzzle.

Puzzle 1:

7 - - - - - - - 6

- - 3 - - 2 - - -

8 - 1 3 4 - 9 - -

2 - - - - 3 - 8 -

- 8 - - - - - 6 -

- 4 - 1 - - - - 2

- - 2 - 8 1 6 - 4

- - - 2 - - 3 - -

1 - - - - - - - 7

Puzzle 2:

- 8 6 - - - - 1 -

7 - 5 - - 8 - - -

- - 2 - - - - - 4

- - 4 - - - 3 6 -

- - 8 5 - - - - -

- - - 1 - - - - -

- - - - - - - 8 1

8 - - - - 4 2 - -

- 2 - 6 - 3 - - -

 

Puzzle 3:

2 9 - - - - - - -

4 - - 9 - - 7 - 3

- - - - - 5 - 1 -

- 2 1 - - 3 - - -

- - 8 7 - - - - 5

- - - - - - 6 2 -

- - 2 - - 6 5 - 4

- - - 3 - - - 9 -

- - 4 - - 1 - - -:

 

Language

You may use any language to implement this project. However, we recommend C, C++, or Java. We should be able to easily compile (or interpret) your program for testing on any Linux machine in the CS department. It is your responsibility to make sure your program compiles and runs. If it does not, you will not get any credit. You may not search online for code for solving Sudoku puzzles. You may look at the book code for CSP algorithms online. However, if you do use them, you must cite the code. If you violate this trust you could receive a zero.

What to turn in

1.       Copies of all your source code and a single driver that runs the three given puzzles in turn. Preferably a single zip or jar file.

2.       The performance results (the output of the algorithm runs.).

Due date

March 7th, before midnight.